package com.ruoyi.project.mahjong.service.task;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.mahjong.domain.TCost;
import com.ruoyi.project.mahjong.domain.TOpenTable;
import com.ruoyi.project.mahjong.domain.enu.CostUnit;
import com.ruoyi.project.mahjong.domain.enu.DeskStatusEnum;
import com.ruoyi.project.mahjong.service.ITCostService;
import com.ruoyi.project.mahjong.service.ITOpenTableService;
import com.ruoyi.project.remake.domain.enmu.YesORNoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class ChargTask {


    @Autowired
    private ITOpenTableService openTableService;
    @Autowired
    private ITCostService costService;


    public void calOptableCask() {
        TOpenTable openTable = new TOpenTable();
        openTable.setStatus(YesORNoEnum.YES.getValue().toString());
        List<TOpenTable> tOpenTableList = openTableService.selectTOpenTableList(openTable);
        for (TOpenTable tOpenTable : tOpenTableList) {
            TCost cost = costService.selectTCostById(Long.valueOf(tOpenTable.getCostType()));//计费类型
            Long costType = cost.getCostId();
            BigDecimal money=new BigDecimal(0);
            if (costType.toString().equals(CostUnit.MINUTE.getValue())) {//按照分钟计时
                money=coverByMinute(tOpenTable,cost);
            }
            if (costType.toString().equals(CostUnit.CHANG.getValue())) {//按照按包场计费
                money=coverByChang(tOpenTable,cost);
            }
            if (costType.toString().equals(CostUnit.FEN_SHI_DUAN_BAO_CHANG.getValue())) {//按分时段包段计费
                money=coverByFen(tOpenTable,cost);
            }
            tOpenTable.setMoney(money);
            openTableService.updateTOpenTable(tOpenTable);

        }


    }


    public void caclDesk(String orderSn) {
        TOpenTable openTable = new TOpenTable();
        openTable.setStatus(DeskStatusEnum.OK.getValue());
        openTable.setBak2(orderSn);
        List<TOpenTable> tOpenTableList = openTableService.selectTOpenTableList(openTable);
        for (TOpenTable tOpenTable : tOpenTableList) {
            TCost cost = costService.selectTCostById(Long.valueOf(tOpenTable.getCostType()));//计费类型
            Long costType = cost.getCostId();
            BigDecimal money = new BigDecimal(0);
            if (costType.toString().equals(CostUnit.MINUTE.getValue())) {//按照分钟计时
                money = coverByMinute(tOpenTable,cost);
            }
            if (costType.toString().equals(CostUnit.CHANG.getValue())) {//按照按包场计费
                money = coverByChang(tOpenTable,cost);
            }
            if (costType.toString().equals(CostUnit.FEN_SHI_DUAN_BAO_CHANG.getValue())) {//按分时段包段计费
                money = coverByFen(tOpenTable,cost);
            }
            tOpenTable.setMoney(money);
            openTableService.updateTOpenTable(tOpenTable);
        }
    }

    /**
     * 按照分场地试时段计费
     * @param tOpenTable
     * @return
     */
    private BigDecimal coverByFen(TOpenTable tOpenTable,TCost cost) {
        TCost tCost=new TCost();
        tCost.setBak2(cost.getBak2());
        tCost.setBak1(cost.getBak1());
        tCost.setCostId(Long.valueOf(CostUnit.FEN_SHI_DUAN_BAO_CHANG.getValue()));
        List<TCost> costList=costService.selectTCostList(tCost);
        TCost dateCost=getTCost(costList);
        if(ObjectUtil.isNull(dateCost)){
            return coverByMinute(tOpenTable,cost);
        }
       return  coverByFenAndChang(tOpenTable,cost);

    }

    private BigDecimal coverByFenAndChang(TOpenTable tOpenTable, TCost cost) {
        Date startTime= DateUtils.parseDate(tOpenTable.getOpenStartTime());
        if(ObjectUtil.isNull(startTime)){
            startTime=new Date();
        }
        Long time=DateUtils.getNowDate().getTime()-startTime.getTime();
        //分钟数量
        Double costUnit= Double.valueOf(cost.getCostUnit());
        //得到分钟的单位数量
        Long mintue=(time/60000)+1;
        //得到多少个时间端然后加1
        int number=(int)(mintue/costUnit+1);

        BigDecimal money=new BigDecimal(cost.getMoney()*number).setScale(BigDecimal.ROUND_HALF_UP,2);
        return money;
    }


    /**
     * 获取当时时间段属于那个报时
     * @param costList
     * @return
     */
    private TCost getTCost(List<TCost> costList) {

        for(TCost cost:costList){
            String startTime=cost.getStartTime();//开始时间
            String endTime=cost.getEndTime();//
            Calendar calendar=Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY,Integer.valueOf(startTime.split(":")[0]));
            calendar.set(Calendar.MINUTE,Integer.valueOf(startTime.split(":")[1]));
            Date statDate=calendar.getTime();
            calendar.set(Calendar.HOUR_OF_DAY,Integer.valueOf(endTime.split(":")[0]));
            calendar.set(Calendar.MINUTE,Integer.valueOf(endTime.split(":")[1]));
            Date endDate=calendar.getTime();
            Date timeNow=new Date();
            if(timeNow.getTime()>statDate.getTime()&&timeNow.getTime()<endDate.getTime()){
                return cost;
            }

        }
        return null;


    }

    /**
     * 按照包场收费
     * @param tOpenTable
     * @return
     */
    private BigDecimal coverByChang(TOpenTable tOpenTable,TCost cost) {
        Date startTime= DateUtils.parseDate(tOpenTable.getOpenStartTime());
        if(ObjectUtil.isNull(startTime)){
            startTime=new Date();
        }
        Long time=DateUtils.getNowDate().getTime()-startTime.getTime();
        Double costUnit= Double.valueOf(cost.getCostUnit());//分钟数量
        int mintue=(int)((time/60000/costUnit)+1);//得到积分的单位数量
        BigDecimal money=new BigDecimal(cost.getMoney()*mintue).setScale(BigDecimal.ROUND_HALF_UP,2);
        return money;
    }

    /**
     * 按照分钟计费
     * @param tOpenTable
     * @return
     */
    private BigDecimal coverByMinute(TOpenTable tOpenTable,TCost cost) {
        Date startTime= DateUtils.parseDate(tOpenTable.getOpenStartTime());
        Long time=DateUtils.getNowDate().getTime()-startTime.getTime();
        Integer costUnit= Integer.valueOf(cost.getCostUnit());//分钟数量
        Long mintue=(time/60000/costUnit)+1;//得到积分的单位数量
        BigDecimal money=new BigDecimal(cost.getMoney()*mintue).setScale(BigDecimal.ROUND_HALF_UP,2);
        return money;
    }
}
